// components/course-item/index.js
import {OrderModel} from '../../model/order'
var order = new OrderModel()
const { config } = require("../../config")

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    model:Object
  },

  /**
   * 组件的初始数据
   */
  data: {
    system:config.system
  },

  /**
   * 组件的方法列表
   */
  methods: {
    payAction:function(e){
      if(this.properties.model.isBuy===1){
        return
      }
      order.course(this.properties.model.id).then(
        res=>{
          if (res.code===0) {
            this.pay(res.data)
          }
        }
      )
      console.log(e)
    },

    pay(orderId){
      order.pay(orderId).then(
        res=>{
          if (res.code===0) {
            if (res.data===null) {
              this.triggerEvent("upData")
              return
            }
            console.log(res.data)
            var data = JSON.parse(res.data);
            wx.requestPayment({
              timeStamp: data.timeStamp,
              nonceStr: data.nonceStr,
              package: data.package,
              signType: 'MD5',
              paySign: data.paySign,
              success (resp) {
                wx.showToast({
                  title: '支付成功',
                  icon: 'none'
                })
                this.triggerEvent("upData")
               },
            })
          }
        }
      )
    }
  }
})
